package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.base.constant.PetHomeConstant;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.base.util.MD5Utils;
import cn.itsource.pethome.base.util.StrUtils;
import cn.itsource.pethome.user.domain.LoginInfo;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDto;
import cn.itsource.pethome.user.mapper.LoginInfoMapper;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

        @Autowired
        private UserMapper userMapper;

        @Autowired
        private LoginInfoMapper loginInfoMapper;

        @Autowired
        private RedisTemplate redisTemplate;

        @Transactional
        @Override
        public void register(UserDto userDto) {
            //校验前端传递的数据
            checkUserDto(userDto);
            //创建LoginInfo对象
            LoginInfo loginInfo = userDto2LoginInfo(userDto);
            //保存之前----通过用户名和状态去判断数据库中是否唯一，如果存在了，就不能保存
            //保存LoginInfo
            loginInfoMapper.save(loginInfo);
            //把loginInfo对象转为user对象
            User user = loginInfo2User(loginInfo);
            //保存User对象
            userMapper.save(user);

        }

        /*把登录信息转为user对象*/
        private User loginInfo2User(LoginInfo loginInfo) {
            User user = new User();
            //只要属性就能拷贝
            BeanUtils.copyProperties(loginInfo, user);
            //0: 待激活    1:正常
            user.setState(PetHomeConstant.OK);
            user.setLoginInfo(loginInfo);
            return user;
         }

        /*把临时对象userDto转为登录信息对象*/
        private LoginInfo userDto2LoginInfo(UserDto userDto){
            LoginInfo loginInfo = new LoginInfo();
            //设置用户名
            loginInfo.setUsername(userDto.getPhone());
            //设置电话
            loginInfo.setPhone(userDto.getPhone());
            //设置盐值
            loginInfo.setSalt(StrUtils.getComplexRandomString(10));
            //设置密码
            loginInfo.setPassword(MD5Utils.encrypByMd5(userDto.getPassword() + loginInfo.getSalt()));
            //标记前端用户
            loginInfo.setType(true);
            return loginInfo;
        }

        /*校验前端传递的所有信息*/
        private void checkUserDto(UserDto userDto){
            //校验前端传递的数据是否为空
            if(StringUtils.isEmpty(userDto.getCode())||StringUtils.isEmpty(userDto.getPhone())||
                    StringUtils.isEmpty(userDto.getPassword())|| StringUtils.isEmpty(userDto.getConfigPassword())){
                throw new RuntimeException("请填写完整信息");
            }
            //校验手机号是否已被注册
            User user = userMapper.loadByPhone(userDto.getPhone());
            if(user != null){
                throw new RuntimeException("该手机号码已被注册");
            }
            //校验2次密码是否一致
            if(!userDto.getPassword().equals(userDto.getConfigPassword())){
                throw new RuntimeException("两次密码不一致");
            }
            //校验判断验证码是否过期    78De:3424234242
            String codeValue = (String)redisTemplate.opsForValue().
                    get(PetHomeConstant.REGISTER_VERIFICATION_CODE + ":" + userDto.getPhone());
            if(StringUtils.isEmpty(codeValue)){
                throw new RuntimeException("验证码已过期，请重新发送");
            }
            //校验前端传递的验证码是否和redis中存储的验证码一致
            String code = codeValue.split(":")[0];
            if(!code.toLowerCase().equals(userDto.getCode().toLowerCase())){
                throw new RuntimeException("验证码错误");
            }
        }
}
